package com.dingding.common.core.util;

import java.io.UnsupportedEncodingException;
import java.util.HashMap;
import java.util.Iterator;
import java.util.Map;
import org.apache.log4j.Logger;
import org.dom4j.Attribute;
import org.dom4j.Document;
import org.dom4j.DocumentException;
import org.dom4j.DocumentHelper;
import org.dom4j.Element;
import com.thoughtworks.xstream.XStream;
import com.thoughtworks.xstream.io.xml.DomDriver;
import com.thoughtworks.xstream.io.xml.XmlFriendlyReplacer;
import com.thoughtworks.xstream.io.xml.XppDriver;

/**
 * @ClassName: XMLParse 
 * @Description: 微信端接收发送xml的处理
 * @author fengshuonan
 * @date 2015年10月15日 下午12:48:51 
 */
@SuppressWarnings("all")
public class XMLParse {

	private static Logger log = Logger.getLogger(XMLParse.class);
	
	/**
	 * @Title: toXML
	 * @Description: 消息bean转化成xml
	 * @param abstractMessage 实体bean
	 * @author fengshuonan
	 * @return String bean的xml形式
	 * @throws
	 */
	public static String toXML(Object obj)
	{
		XStream xstream = new XStream(new XppDriver(new XmlFriendlyReplacer("_-","_")));
		xstream.autodetectAnnotations(true);
		
		//处理中文编码
		String temp = xstream.toXML(obj);
		try {
			String result = new String(temp.getBytes("UTF-8"), "ISO8859-1");
			return result;
		} catch (UnsupportedEncodingException e) {
			log.error("处理中文编码错误！WechatPayApi:placeAnOrder",e);
		}
		
		return "";
	}
	
	/**
	 * @Title: toXML
	 * @Description: 消息bean转化成xml
	 * @param abstractMessage 实体bean
	 * @param encode 编码
	 * @author fengshuonan
	 * @return String bean的xml形式
	 * @throws
	 */
	public static String toXML(Object obj,String encode){
		
		XStream xstream = new XStream(new XppDriver(new XmlFriendlyReplacer("_-","_")));
		xstream.autodetectAnnotations(true);
		
		//处理中文编码
		String temp = xstream.toXML(obj);
		try {
			String result = new String(temp.getBytes("UTF-8"), encode);
			return result;
		} catch (UnsupportedEncodingException e) {
			log.error("处理中文编码错误！WechatPayApi:placeAnOrder",e);
		}
		
		return "";
	}
	
	/**
	 * @Title: toObject
	 * @Description: 将xml转化成实体bean
	 * @param xml bean的xml形式
	 * @author fengshuonan
	 * @return AbstractMessage 实体bean
	 * @throws
	 */
	@SuppressWarnings("unchecked")
	public static <T> T toObject(String xml,Class<T> clazz)
	{
		XStream xs = new XStream(new DomDriver());  
		xs.setMode(XStream.NO_REFERENCES);
		xs.ignoreUnknownElements();//忽略不知道的字段
		xs.processAnnotations(new Class[]{clazz});
		return (T) xs.fromXML(xml);
	}
	
	
	/**
	 * @Description: 把xml解析成一个map
	 * @author fengshuonan
	 */
	public static Map<String,String> xmlToMap(String xml)
	{
		
		Map<String,String> hashMap = null;
		
		try {
			Document document = DocumentHelper.parseText(xml);
			Element rootElement = document.getRootElement();
			
			hashMap = new HashMap<String,String>();
			
	        for(Iterator it=rootElement.elementIterator();it.hasNext();){        
	        	   Element element = (Element)it.next();
	        	   String name = element.getName();
		           String text=element.getText();  
		           hashMap.put(name, text);
	        }
		} catch (DocumentException e) {
			log.error("解析xml的时候错误！XMLParse:xmlToMap()",e);
		}
		
		return hashMap;
	}

}
